package com.ourteam.workspace.api;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.SystemUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractTreeBuilderController;
import com.ourteam.frame.web.TreeParamModel;
import com.ourteam.frame.web.TreeViewNodeBean;

@Controller
@RequestMapping("/api/WORKSPACE/workspaceTree")
public class WorkspaceTreeBuilderAPI extends AbstractTreeBuilderController<WorkspaceTreeBuilder> {

	@Override
	protected WorkspaceTreeBuilder getTreeBuilder(HttpServletRequest request) throws Exception {
		File rootDir = new File(SystemUtils.USER_HOME, this.getConfig("workspace.rootDir"));
		return new WorkspaceTreeBuilder(rootDir,(long)request.getAttribute("workspaceId"));
	}
	
	@RequestMapping({"/buildTree/{workspaceId}/"})
	@ResponseBody
	public List<TreeViewNodeBean> buildTree(@PathVariable long workspaceId,
			@ModelAttribute TreeParamModel treeParamModel, @RequestParam(defaultValue = "0") int expandLevel,
			@RequestParam(required = false) String selectedNodeId, @RequestParam(required = true) String requestId,
			HttpServletRequest request) throws Exception {

		request.setAttribute("workspaceId", workspaceId);
		
		return super.buildTree(treeParamModel, expandLevel, selectedNodeId, requestId, request);

	}

	@Override
	protected Object getInitParameter(HttpServletRequest request) {
		return null;
	}

}
